import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_video_course.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

import 'mine_study_recoder_progress_page.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/2/8.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class MyStudyRecoderPage extends StatefulWidget {
  @override
  _MyStudyRecoderPageState createState() => _MyStudyRecoderPageState();
}

class _MyStudyRecoderPageState extends State<MyStudyRecoderPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getMyStudyRecoderList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("我的学习"),
      ),

      ///填充布局
      body: RefreshIndicator(
        onRefresh: () async {
          await _getMyStudyRecoderList();
          ToastUtils.showToast("已刷新");
          return true;
        },
        child: buildMainWidget(),
      ),
    );
  }

  List<UserStudyCourseBean> _list;

  ///获取我的学习记录
  Future<bool> _getMyStudyRecoderList() async {
    Map<String, dynamic> map = Map();
    //post请求发送
    ResponseInfo responseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.videoStudyListUrl,
      queryParameters: map,
    );
    if (responseInfo.success) {
      List list = responseInfo.data;
      _list = [];
      list.forEach((element) {
        UserStudyCourseBean beanVideoCategory =
            UserStudyCourseBean.fromMap(element);
        _list.add(beanVideoCategory);
      });
    }
    if (mounted) {
      setState(() {});
    }

    return true;
  }

  buildMainWidget() {
    if (_list == null || _list.length == 0) {
      return Center(
        child: NoDataWidget(
          text: "暂无学习记录",
          clickCallBack: () {
            _getMyStudyRecoderList();
          },
        ),
      );
    }
    //一个列表 切记不要再使用 Scaffold
    //否则会拦截事件 失去滑动联动效果
    return ListView.builder(
      //去除内边距
      padding: EdgeInsets.all(0),
      //列表子Item个数
      itemCount: _list.length,
      //每个子Item的布局构建
      itemBuilder: (BuildContext context, int index) {
        UserStudyCourseBean beanVideoCourse = _list[index];
        return InkWell(
          onTap: () {
            NavigatorUtils.pushPage(
                context,
                MyStudyRecoderProgressPage(
                  courseBean: beanVideoCourse,
                ));
          },
          child: buildBodyWidget(index),
        );
      },
    );
  }

  buildBodyWidget(int index) {
    UserStudyCourseBean beanVideoCourse = _list[index];
    return Container(
      color: Colors.white,
      padding: EdgeInsets.all(12),
      margin: EdgeInsets.only(top: 3),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            alignment: Alignment.center,
            margin: EdgeInsets.only(top: 4, right: 6),
            padding: EdgeInsets.only(left: 6, right: 6),
            height: 18,
            decoration: BoxDecoration(
                color: Colors.blueGrey[200],
                borderRadius: BorderRadius.all(Radius.circular(4))),
            child: Text(
              "${index + 1}",
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(fontSize: 12),
            ),
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  "${beanVideoCourse.courseName}",
                  style: TextStyle(
                    fontWeight: FontWeight.w500,
                    fontSize: 16,
                  ),
                ),
                SizedBox(
                  height: 4,
                ),
                Text(
                  "参与学习时间 ${beanVideoCourse.createTime}",
                  style: TextStyle(
                    color: Color(0xff666666),
                    fontSize: 14,
                  ),
                )
              ],
            ),
          ),
          Container(
              margin: EdgeInsets.only(top: 2),
              child: Icon(Icons.arrow_right_rounded)),
        ],
      ),
    );
  }
}
